package com.chzu.utils;

import com.chzu.pojo.SysUser;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName SecurityUtil
 * @Description TODO
 * @Date 2022/01/27 23:26
 */
public class SecurityUtil {
    /**
     * 获取用户名
     * @return
     */
    public static String getUsername() {
        try {
            return ((SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取用户ID
     * @return
     */
    public static Integer getUserId() {
        try {
            return ((SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取用户信息
     * @return
     */
    public static SysUser getUserInfo() {
        try {
            SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            user.setPassword(null);
            return user;
        } catch (Exception e) {
            return null;
        }
    }
}
